<template>
  <div class="login-wrapper">
    <el-form
      ref="loginForm"
      :model="form"
      :rules="rules"
      class="login-form ele-bg-white"
      size="large"
      @keyup.enter.native="doSubmit"
    >
      <div class="head">
        <el-tooltip content="功能未完成" placement="top">
          <img class="Tooltip-open" src="~@/assets/image/account-trigon.png"/>
        </el-tooltip>
        <div class="Tooltip">
          <span class="content">扫码登录更便捷</span>
          <span class="arrow"></span>
        </div>
      </div>
      <h4>密码登录</h4>
      <el-form-item prop="username">
        <el-input
          v-model="form.username"
          clearable
          placeholder="请输入登录账户"
          prefix-icon="el-icon-user"
        />
      </el-form-item>
      <el-form-item prop="password">
        <el-input
          v-model="form.password"
          placeholder="请输入登录密码"
          prefix-icon="el-icon-lock"
          show-password
        />
      </el-form-item>
      <el-form-item prop="captcha">
        <div class="login-input-group">
          <el-input
            v-model="form.captcha"
            placeholder="请输入验证码"
            prefix-icon="el-icon-_vercode"
          />
          <img
            v-if="captcha"
            :src="captcha"
            alt=""
            class="login-captcha"
            @click="changeCode"
          />
        </div>
      </el-form-item>
      <div class="el-form-item">
        <el-checkbox v-model="form.remember">记住密码</el-checkbox>
      </div>
      <div class="el-form-item">
        <el-button
          :loading="loading"
          class="login-btn"
          size="large"
          type="primary"
          @click="doSubmit"
        >
          {{ loading ? "登录中" : "登录" }}
        </el-button>
      </div>
    </el-form>
    <div class="login-copyright">
      <p>用创新和善意推动世界前进</p>
      <br/>
      <p>
        © 2021&nbsp;<a href="https://beian.miit.gov.cn/" target="_Blank"
      >京ICP备10000000号-1</a
      >&nbsp;版权所有
      </p>
    </div>
    <!-- 实际项目去掉这段 -->
    <div
      class="hidden-xs-only"
      style="position: absolute; left: 30px; top: 20px"
    >
      <a href="#" target="_blank"
      ><img
        height="24px"
        src=""
      /></a>
    </div>
  </div>
</template>

<script>
import setting from "@/config/setting";

export default {
  name: "Login",
  data() {
    return {
      // 登录框方向, 0居中, 1居右, 2居左
      direction: 0,
      // 加载状态
      loading: false,
      // 表单数据
      form: {
        username: "",
        password: "",
        domain: window.location.hostname,
        captcha: "",
        token: "",
        remember: true,
      },
      // 表单验证规则
      rules: {
        username: [{required: true, message: "请输入账户", trigger: "blur"}],
        password: [{required: true, message: "请输入密码", trigger: "blur"}],
        captcha: [{required: true, message: "请输入验证码", trigger: "blur"}],
      },
      // 验证码base64数据
      captcha: "",
      // 验证码内容, 实际项目去掉
      text: "",
    };
  },
  mounted() {
    if (setting.takeToken()) {
      this.goHome();
    } else {
      this.changeCode();
    }
  },
  methods: {
    /* 提交 */
    doSubmit() {
      this.$refs["loginForm"].validate((valid) => {
        if (!valid) {
          return false;
        }
        this.loading = true;
        let formData = new FormData();
        for (let key in this.form) {
          formData.append(key, this.form[key]);
        }
        this.$http
          .post("/login/index", formData)
          .then((res) => {
            this.loading = false;
            if (res.data.code === 0) {
              this.$message.success("登录成功");
              this.$store
                .dispatch("user/setToken", {
                  token: 'Bearer ' + res.data.access_token,
                  remember: this.form.remember,
                })
                .then(() => {
                  this.goHome();
                });
            } else {
              this.changeCode();
              this.$message.error(res.data.msg);
            }
          })
          .catch(() => {
            this.loading = false;
            this.$message.error("网络异常请稍后重试!");
          });
      });
    },
    /* 跳转到首页 */
    goHome() {
      const query = this.$route.query;
      const path = query && query.from ? query.from : "/";
      this.$router.push(path).catch(() => {
      });
    },
    /* 更换图形验证码 */
    changeCode() {
      // 这里演示的验证码是后端返回base64格式的形式, 如果后端地址直接是图片请参考忘记密码页面
      this.$http
        .get("/captcha", {params: {t: new Date().getTime()}})
        .then((res) => {
          if (res.data.code === 0) {
            this.captcha = res.data.url;
            this.form.token = res.data.token;
          } else {
            this.$message.error(res.data.msg);
          }
        })
        .catch((e) => {
          this.$message.error(e.message);
        });
    },
  },
};
</script>

<style scoped>
.login-wrapper {
  padding: 50px 20px;
  position: relative;
  box-sizing: border-box;
  background-image: url("~@/assets/image/login-bg.svg");
  background-size: cover;
  background-repeat: round;
  min-height: 100vh;
}

.login-form {
  margin: 0 auto;
  width: 420px;
  height: 520px;
  max-width: 100%;
  padding: 25px 30px;
  position: relative;
  box-shadow: rgba(90, 100, 170, 0.12) 0px 2px 24px 0px;
  box-sizing: border-box;
  border-radius: 10px;
  z-index: 2;
}

.login-wrapper .head {
  height: 72px;
  position: relative;
}

.login-wrapper .head .Tooltip-open {
  width: 72px;
  height: 72px;
  position: absolute;
  top: -25px;
  right: -30px;
}

.login-wrapper .head .Tooltip {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
  color: rgb(47, 130, 248);
  font-size: 14px;
  font-variant: tabular-nums;
  line-height: 1.5715;
  list-style: none;
  font-feature-settings: "tnum";
  position: absolute;
  right: 38px;
  z-index: 1070;
  display: block;
  max-width: 250px;
  visibility: visible;
}

.login-wrapper .head .Tooltip .content {
  min-width: 30px;
  min-height: 32px;
  padding: 6px 8px;
  text-align: left;
  text-decoration: none;
  word-wrap: break-word;
  border-radius: 0;
  box-shadow: unset;
  border: 1px solid rgba(70, 90, 250, 0.06);
  background: rgba(70, 90, 250, 0.06);
  color: rgb(70, 90, 250);
  white-space: nowrap;
}

.login-form-right .login-form {
  margin: 0 15% 0 auto;
}

.login-form-left .login-form {
  margin: 0 auto 0 15%;
}

.login-form h4 {
  text-align: left;
  margin: 0 0 25px 0;
}

.login-form > .el-form-item {
  margin-bottom: 25px;
}

.login-input-group {
  display: flex;
  align-items: center;
}

.login-input-group ::v-deep .el-input {
  flex: 1;
}

.login-captcha {
  height: 38px;
  width: 102px;
  margin-left: 10px;
  border-radius: 4px;
  border: 1px solid #dcdfe6;
  text-align: center;
  cursor: pointer;
}

.login-captcha:hover {
  opacity: 0.75;
}

.login-btn {
  display: block;
  width: 100%;
}

.login-oauth-icon {
  color: #fff;
  padding: 5px;
  margin: 0 10px;
  font-size: 18px;
  border-radius: 50%;
  cursor: pointer;
}

.login-copyright {
  color: rgba(0, 0, 0, 0.25);
  height: 100px;
  text-align: center;
  position: relative;
  z-index: 100;
  background-image: url("~@/assets/image/login-bg-line.png");
  background-size: cover;
  background-repeat: round;
}

@media screen and (min-height: 550px) {
  .login-form {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translateX(-50%);
    margin-top: -260px;
  }

  .login-form-right .login-form,
  .login-form-left .login-form {
    left: auto;
    right: 15%;
    transform: translateX(0);
    margin: -260px auto auto auto;
  }

  .login-form-left .login-form {
    right: auto;
    left: 15%;
  }

  .login-copyright {
    position: absolute;
    bottom: 0;
    right: 0;
    left: 0;
  }
}

@media screen and (max-width: 900px) {
  .login-form {
    width: 328px;
  }

  .login-form-right .login-form,
  .login-form-left .login-form {
    left: 50%;
    right: auto;
    transform: translateX(-50%);
    margin-right: auto;
  }
}
</style>
